import pymongo

import false_email


class ToMongo(object):
    client = pymongo.MongoClient('localhost')
    db = client['lives']
    f_email = false_email.FalseEmail()

    def to_mongo(self, data_csv, platform):
        data_dict = self.get_dict(data_csv)
        try:
            self.db[platform].insert_many(data_dict)
        except:
            content = '{}写入mongo失败'.format(platform)
            title = 'mongo写入失败'
            self.f_email.send_email(title, content)
            print(content)
            return

    def get_dict(self, data_csv):
        data = data_csv.to_dict(orient='records')
        return data
